/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik.gui.swing;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;
import ch.bfh.ti.kybernetik.gui.common.RoboterColorManager;
import ch.bfh.ti.kybernetik.simulator.controller.Simulator;
import ch.bfh.ti.kybernetik.simulator.controller.roboter.RoboterController;
import ch.bfh.ti.kybernetik.simulator.controller.roboter.RobotorControllerFactory;

public class GuiCommand extends JFrame {

	private JButton addRoboterButton;

	private Simulator simulator;

	private RoboterColorManager roboterColor = RoboterColorManager.getInstance();

	public GuiCommand(Simulator simulator) {
		this.simulator = simulator;

		setTitle("Simulator Controller");
		setSize(300, 200);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.initGUI();
		// this.setAlwaysOnTop(true);
	}

	public final void initGUI() {
		JPanel panel = new JPanel(new MigLayout());
		getContentPane().add(panel);

		panel.add(new JLabel("A Text field"));
		panel.add(new JTextField(100), "wrap");

		panel.add(new JLabel("Add Roboter"));
		panel.add(getAddRandomRoboterButton(), "wrap");

		panel.add(new JLabel("Simulator Speed"));
		panel.add(getSimulatorSpeedInputComponent(), "wrap");

		panel.add(new JLabel("Delta T"));
		panel.add(getDeltaTInputComponent(), "wrap");

		panel.add(new JLabel("Delta T"));
		panel.add(getDeltaTInputComponent(), "wrap");

		// panel.add(quitButton);
	}

	public JButton getAddRandomRoboterButton() {
		if (addRoboterButton == null) {
			addRoboterButton = new JButton("Create Random Roboter");
			// quitButton.setBounds(50, 60, 80, 30);

			addRoboterButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					RoboterController aRoboterController = RobotorControllerFactory.createRoboterController();
					simulator.addRoboterController(aRoboterController);
					Random numGen = new Random();
					roboterColor.putRoboterController(aRoboterController.getRoboter(), new Color(numGen.nextInt(256), numGen.nextInt(256),
							numGen.nextInt(256)));
				}
			});
		}
		return addRoboterButton;

	}

	public JComponent getSimulatorSpeedInputComponent() {
		JSlider simulatorSpeedSlider = new JSlider(JSlider.HORIZONTAL, 1, 500, simulator.getSimulatorSpeed());
		simulatorSpeedSlider.setPaintLabels(true);
		simulatorSpeedSlider.setMajorTickSpacing(25);
		simulatorSpeedSlider.setPaintTicks(true);
		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put(new Integer(1), new JLabel("Fast"));
		labelTable.put(new Integer(500), new JLabel("Slow"));
		simulatorSpeedSlider.setLabelTable(labelTable);
		simulatorSpeedSlider.setPaintLabels(true);
		simulatorSpeedSlider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				simulator.setSimulatorSpeed(source.getValue());
			}
		});
		return simulatorSpeedSlider;
	}

	public JComponent getDeltaTInputComponent() {
		final JTextField textField = new JTextField(100);
		textField.setText(String.valueOf(simulator.getDeltaT()));
		textField.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String newText = textField.getText();
				try {
					Double newValue = Double.valueOf(newText);
					simulator.setDeltaT(newValue);
				} catch (NumberFormatException nfe) {
					System.out.println("Invalid Input for Delta T: " + nfe);
				}
			}
		});
		return textField;

	}
}
